/**
 * <b>Cookie class:</b>
 * <hr color=grey size=1>
 *
 * Get all pair information of current document object
 *
 * @author zozoh  created @ 2006
 *
 */
ihelpuoo.http.Cookie = function()
{
	this._pairs = new ActiveXObject("Scripting.Dictionary");
	this.valueOf(document.cookie);
}

ihelpuoo.http.Cookie.prototype
=
{
	valueOf: function(content)
	{
		if(!content) return;
		var paires = content.split(";");
		for(var i=0;i<paires.length;i++)
		{
			if(paires[i].length>0)
			{
				var pair = new ihelpuoo.http.Pair(paires[i]);
				this.setPair(pair);
			}
		}
	}
	
	,
	getPair: function(name)
	{
		if(!this._pairs.Exists(name)) return null;
		
		return this._pairs.Item(name);
	}
	
	,
	setPair: function(pair)
	{
		if(this._pairs.Exists(pair.name))
		{
			this._pairs.Item(pair.name) = pair;
		}
		else
		{
			this._pairs.Add(pair.name,pair);
		}
	}
	
	,
	/**
	 * It will create or update a pair's value.
	 * 
	 * @param name: name
	 * @param value: value
	 */
	setValue: function(name,value)
	{
		var pair = this.getPair(name);
		if(pair)
			pair.value = value;
		else
		{
			pair = new ihelpuoo.http.Pair();
			pair.name = name;
			pair.value = value;
			this._pairs.Add(pair.name,pair);	
		}
	}
	
	,
	/**
	 * Get a value of a pair.
	 * 
	 * @param name: pair name
	 */
	getValue: function(name)
	{
		var pair = this.getPair(name);
		if(pair)
			return pair.value;
		
		return null; 
	}
	
	,
	remove: function(name)
	{
		if(this._pairs.Exists(name))
		{
			this._pairs.Remove(name);
			document.cookie = name + "=;expires=Fri, 20 Oct 1900 03:02:30 UTC";
		}
	}
	
	,
	toString: function()
	{
		var vs = (new VBArray(this._pairs.Items())).toArray();
		var re = "";
		for(var i=0;i<vs.length;i++)
		{
			re+= vs[i].toString();
		}
		
		return re;
	}
	
	,
	save: function()
	{
		var vs = (new VBArray(this._pairs.Items())).toArray();
		for(var i=0;i<vs.length;i++)
		{
			document.cookie = vs[i].toString();
		}
	}

	,
	/**
	 * Output the dumpping string for tracing.
	 * 
	 * @return message string.
	 */
	dump: function()
	{
		return this.toString();
	}
}

/**
 * <b>Pair class:</b>
 * <hr color=grey size=1>
 *
 * Get all pair information of current document object
 * 
 */
ihelpuoo.http.Pair = function(content)
{
	this.name=null;
	this.value=null;
	this.expires = null;
	this.domain = null;
	this.path = null;
	this.secure = false;

	if(content)
		this.valueOf(content);
}

ihelpuoo.http.Pair.prototype
=
{
	valueOf: function(content)
	{
		var att = new ihelpuoo.util.Attribute();
		att.valueOf(content);
		this.name = att.name;
		this.value = att.value;
	}
	
	,
	toString: function()
	{
		var re = this.name + "=" + this.value + ";";

		if(this.expires)
			re += "expires=" + this.expires + ";";
		if(this.domain)
			re += "domain=" + this.domain + ";";
		if(this.path)
			re += "path=" + this.path + ";";
		if(this.secure)
			re += "secure;";
		return re;
	}
	
	,
	/**
	 * Output the dumpping string for tracing.
	 * 
	 * @return message string.
	 */
	dump: function()
	{
		return this.toString();
	}
}